package org.dicr.radius.test;

import java.io.*;
import java.net.*;
import java.util.*;

import org.dicr.radius.*;
import org.dicr.radius.RadiusDictionary.*;
import org.dicr.radius.dict.*;
import org.slf4j.*;

public class Dictionary {

	private static final Logger log = LoggerFactory.getLogger(Dictionary.class);

	public static void testLoadDefault() throws Exception {
		long timeStart;
		long timeEnd;
		
		timeStart = System.currentTimeMillis();
		XMLDictionary.getInstance().searchVendor(0);
		timeEnd = System.currentTimeMillis();
		log.debug("time=" + ((timeEnd-timeStart) / (float)1000) + " seconds");

		timeStart = System.currentTimeMillis();
		FreeRadiusDictionary.getInstance().searchVendor(0);
		timeEnd = System.currentTimeMillis();
		log.debug("time=" + ((timeEnd-timeStart) / (float)1000) + " seconds");
	}

	private static void testExport() throws Exception {
		URL targetBase = Dictionary.class.getResource("Dictionary.class");
		URL testUrl = null;
		AbstractDictionary dic = null;
		OutputStream out = null;

		log.info("-------- exporting XML");
		dic = XMLDictionary.getInstance();
		testUrl = new URL(targetBase, "dictionary-test.xml");
		out = new FileOutputStream(testUrl.getFile());
		dic.exportData(out);
		out.close();
		dic = XMLDictionary.getInstance(testUrl);
		dic.searchVendor(0);

		log.info("-------- exporting FreeRadius");
		dic = FreeRadiusDictionary.getInstance(FreeRadiusDictionary.class.getResource("../../../../dictionary.freeradius/dictionary"));
		testUrl = new URL(targetBase, "dictionary-test.txt");
		out = new FileOutputStream(testUrl.getFile());
		dic.exportData(out);
		out.close();
		dic = FreeRadiusDictionary.getInstance(testUrl);
		dic.searchVendor(0);
	}

	private static void parseFreeRadiusCatalog() throws Exception {
		AbstractDictionary frDic = FreeRadiusDictionary.getInstance(FreeRadiusDictionary.class
				.getResource("../../../../dictionary.freeradius/dictionary"));
		
		URL base = FreeRadiusDictionary.class.getResource("FreeRadiusDictionary.class");
		OutputStream out = new FileOutputStream(new URL(base, "dictionary.txt").getFile());
		frDic.exportData(out);
		out.close();
		
		AbstractDictionary xmlDic = XMLDictionary.getInstance();
		base = XMLDictionary.class.getResource("XMLDictionary.class");
		xmlDic.importData(frDic);
		out = new FileOutputStream(new URL(base, "dictionary.xml").getFile());
		xmlDic.exportData(out);
		out.close();
	}

	public static void main(final String[] args) throws Exception {
		//parseFreeRadiusCatalog();
		testLoadDefault();
		// final RadiusDictionary dic =
		// FreeRadiusDictionary.getInstance(Dictionary.class.getResource("../../../../../classes/dictionary.freeradius/dictionary"));

		// Tests
		/*
		 * System.out.println("vendor name=" + dic.searchVendor(3041)); System.out.println("attribute name=" + dic.searchAttribute(138, 3076).getName());
		 * System.out.println("value data=" + dic.searchAttribute("AAT-IP-TOS-Precedence").listValues().get("IP-TOS-Precedence-Pri-Five")); /* /*
		 * AttributeDescriptor attr = dic.searchAttribute("AAT-IP-TOS-Precedence"); System.out.println(attr); Map<String, Long> values = attr.listValues(); for
		 * (String name : values.keySet()) { System.out.println(name + " = " + values.get(name)); }
		 */

	}

}
